//
//  CaseDBManager.swift
//  Casecord
//
//  Created by baozi on 2024/1/2.
//

import Foundation
import WCDBSwift
class CaseDBManager: NSObject {
    static let shared: CaseDBManager = CaseDBManager()
    
    private override init() {
        super.init()
    }
    
    func createDBTables() {
        WCDBManager.shared.createTable(table: TicProducter.table, type: TicProducter.self)
    }
    
    // 查询当前所有的记录
    func queryAllRecords() -> [TicProducter] {
       return WCDBManager.shared.queryObjecs(from: TicProducter.table, type: TicProducter.self) ?? []
    }
    
    // 插入一条数据
    func insetRecord(data: TicProducter) {
        let condition = TicProducter.Properties.userId == data.userId
        WCDBManager.shared.update(which: TicProducter.table, on: TicProducter.Properties.all, with: data, where: condition)
    }
    // 删除一条数据
    func deleteRecord(data: TicProducter) {
        let condition = TicProducter.Properties.userId == data.userId
        WCDBManager.shared.delete(from: TicProducter.table, where: condition)
    }
}

final class TicProducter: NSObject, TableCodable {
    static let table: String = "TicNotes_userId_TicProducter"
    var userId: String = ""
    // title
    var title: String = ""
    // 子标题
    var subTitle: String = ""
    // 日期
    var date: String = ""
    // (消费金额/备注)
    var remark: String = ""
    // pics
    var tickets: [String] = []
    
    enum CodingKeys: String, CodingTableKey {
        typealias Root = TicProducter
        case userId
        case title
        case subTitle
        case date
        case remark
        case tickets
        static let objectRelationalMapping = TableBinding(CodingKeys.self) {
            BindColumnConstraint(userId, isPrimary: true)
        }
    }
}

class TicUser: NSObject {
    let userId: String = ""
    let nickName: String = ""
    let gender: String = ""
    let avatar: String = ""
}
